(function($) {
	$.fn.ztreeMenuUtils = function(options, param) {
		if (typeof options == 'string') {
			return $.fn.ztreeMenuUtils.methods[options](this, param);
		}
		// 2.将调用时候传过来的参数和default参数合并
		options = $.extend({}, $.fn.ztreeMenuUtils.defaults, options || {});
		if(options.checkBox && options.radioBox){
			options.radioBox = false;
		}
		if(!options.rootNode)
			options.rootNode = $(this).selector;
		options.loading = top.layer.load(0, {
			shade : [ 0.5, '#fff' ]
		}); // 0代表加载的风格，支持0-2
		if(!options.url){
			var setting = {
					data: {
						simpleData: {
							enable: true
						}
					}
				};
				var zNodes =[
					{ id:1, pid:0, name:"没有权限数据"},
				];
				$.fn.zTree.init($("#"+options.rootNode), setting, zNodes);
				top.layer.close(options.loading);
		}else{
			var setting = {
					check : {
						/** 复选框* */
						enable : options.checkBox,
						chkboxType : {// 复选框
							"Y" : "",// 复选框
							"N" : ""// 复选框
						}
					},
					view : {
						dblClickExpand : false,
					},
					data : {
						simpleData : {// 数据是否采用简单 Array 格式，默认false
							enable : true,
							idKey : "id",
							pIdKey : "pid",
							rootPId : 0
						}
					},
					async : {
						enable : true,
						url : options.url,
						type : "post",
						contentType : "application/json"
					},
					callback : {
						onAsyncSuccess : zTreeOnAsyncSuccess,
						onAsyncError : zTreeOnAsyncError,
						beforeCheck : beforeCheck,
						onClick : onCheckBox
					}
				};
			$.fn.zTree.init($("#" + options.rootNode), setting);
			function zTreeOnAsyncSuccess(event, treeId, treeNode) {
				if(options.checkBox&&options.checkMore){
					if(!options.jsValueCheck){
						initCheckValue();
					}else{
						initJsValueToCheck($(options.jsValueCheck).val());
					}
				};
				top.layer.close(options.loading);
			};
			function zTreeOnAsyncError(event, treeId, treeNode) {

			};
			function beforeCheck(treeId, treeNode) {
				if(options.checkBox && options.checkMore){
					beforeCheckBoxNodesMore(treeNode);
				}else{
					beforeCheckBoxNodes(treeNode);
				}
				return (treeNode.check !== false);
			};
			function beforeCheckBoxNodes(treeNode) {
				if (!treeNode.pid && treeNode.isParent && !options.checkParent) {
					//父节点是否可选，
					return;
				}
				// 选复选框
				if (!treeNode.checked) {
					if(options.checkNodeName)
						$(options.checkNodeName).val(treeNode.name);
					$(options.checkNodes).val(treeNode.id);
				} else {
					$(options.checkNodes).val("");
					if(options.checkNodeName)
						$(options.checkNodeName).val();
				}
			};
			function beforeCheckBoxNodesMore(treeNode) {
				if (!treeNode.pid && treeNode.isParent && !options.checkParent) {
					//父节点是否可选，
					return;
				}
				// 选复选框
				var ids = $(options.checkNodes).val();
				var v;
				if (!treeNode.checked) {
					if(options.checkNodeName){
						v = $(options.checkNodeName).val();
						if(v!=''){
							v = v +options.checkSpilt+treeNode.name;
						}else{
							v = treeNode.name;
						}
					}
					if (ids != '') {
						ids = ids + options.checkSpilt + treeNode.id
					} else {
						ids = treeNode.id
					}
				} else {
					if(options.checkNodeName){
						v = $(options.checkNodeName).val();
						v = functionUtil.replaceAllValue(functionUtil.replaceAllValue(functionUtil.replaceAllValue(v,treeNode.name+options.checkSpilt,""),options.checkSpilt+treeNode.name,""),treeNode.name,"");
					}
					ids = functionUtil.replaceAllValue(functionUtil.replaceAllValue(functionUtil.replaceAllValue(ids,treeNode.id + options.checkSpilt, ""), options.checkSpilt + treeNode.id, ""),treeNode.id, "");
				}
				if(options.checkNodeName)
					$(options.checkNodeName).val(v);
				$(options.checkNodes).val(ids);
			};
			function onCheckBox(e, treeId, treeNode) {
				if(options.checkBox && options.checkMore){
					ischeckNodesMore(treeNode);
				}else{
					ischeckNodes(treeNode);
				}
			};
			function ischeckNodes(treeNode) {
				// 选标题选值
				if (treeNode.pid != null && treeNode.isParent && !options.checkParent) {
					//父节点是否可选，
					return;
				}
				$(options.checkNodes).val(treeNode.id);
				if(options.checkNodeName){
					$(options.checkNodeName).val(treeNode.name);
				}
			};
			function ischeckNodesMore(treeNode) {
				// 选标题选值
				if (treeNode.pid != null && treeNode.isParent && !options.checkParent) {
					//父节点是否可选，
					return;
				}
				var zTree = $.fn.zTree.getZTreeObj(options.rootNode);
				if (treeNode.checked != undefined) {
					zTree.checkNode(treeNode, !treeNode.checked, null, true);
				}
				var nodes = zTree.getCheckedNodes(true), v = "", ids = "";
				for (var i = 0, l = nodes.length; i < l; i++) {
					v += nodes[i].name + options.checkSpilt;
					ids += nodes[i].id+options.checkSpilt;
				}
				if(options.checkNodeName){
					if (v.length > 0)
						v = v.substring(0, v.length - 1);
					$(options.checkNodeName).val(v);
				}
				if (ids.length > 0)
					ids = ids.substring(0, ids.length - 1);
				$(options.checkNodes).val(ids);
				
			};
			function initCheckValue() {
				// 初始化选中。
				var zTree = $.fn.zTree.getZTreeObj(options.rootNode);
				var nodes = zTree.getCheckedNodes(true), ids = "";
				for (var i = 0, l = nodes.length; i < l; i++) {
					ids += nodes[i].id + options.checkSpilt;
				}
				if (ids.length > 0)
					ids = ids.substring(0, ids.length - 1);
				$(options.checkNodes).val(ids);
			};
			function initJsValueToCheck(ids){
				//初始化选中。
				var zTree = $.fn.zTree.getZTreeObj(options.rootNode);
				var nodes = zTree.getNodes()[0].children;//获取子点节,,zTree.getNodes();
				if(ids!=''){
					var arr = ids.split(options.checkSpilt,-1);
					$.each(nodes, function(i, item) {
						$.each(arr, function(i, id) {
							if(item.id==id){
								zTree.checkNode(item, true, true);
							}
						});
					});
				}
			}
		}
	}
	$.fn.ztreeMenuUtils.methods = {
		
	}
	$.fn.ztreeMenuUtils.defaults = {
		url			: null,//异步获取数据
		rootMenu	:null,//根菜单节点
		rootNode	: null,//树根
		checkNodes	: "#checkIds",	//选中的ID保存位置，
		checkNodeName : null,		//选中的节点值显示位置
		checkParent	: false,		//是否可选父节点
		checkBox	: true,		//是否复选框
		checkMore	: true,		//是否多选,只有在复选框的情况下可以，
		checkSpilt	: "#",		//多选时，值通过符号拼接,默认值为#
		radioBox	: false,		//是否显示单选按钮,
		jsValueCheck:null,			//是否用js值初始化
		loading		: "loadinit"	//加载提示框
	};
})(jQuery);

treeMenu = {
	hideMenu : function(rootMenu) {
		$("#"+rootMenu).fadeOut("fast");
		$("body").unbind("mousedown", treeMenu.onBodyDown);
	},
	onBodyDown : function(event) {
		if (!( event.target.id == "channelSelectName" || event.target.id == 'menuChannel' || $(event.target).parents("#menuChannel").length>0)) {
			treeMenu.hideMenu("menuChannel");
		}
		if (!( event.target.id == "channelSelectName2" || event.target.id == 'menuChannel2' || $(event.target).parents("#menuChannel2").length>0)) {
			treeMenu.hideMenu("menuChannel2");
		}
		if (!( event.target.id == "areaSelectName" || event.target.id == 'menuArea' || $(event.target).parents("#menuArea").length>0)) {
			treeMenu.hideMenu("menuArea");
		}
		if (!( event.target.id == "classSelectName" || event.target.id == 'menuClass' || $(event.target).parents("#menuClass").length>0)) {
			treeMenu.hideMenu("menuClass");
		}
	}
}

function showMenu(rootMenu,rootNode,showNode,checkNode) {
	var cityObj = $("#"+showNode);
	var cityOffset = $("#"+showNode).offset();
	var selectIds = $("#"+checkNode).val();
	var zTree = $.fn.zTree.getZTreeObj(rootNode);
	var nodes = zTree.getNodes();
	if(selectIds!=''){
		var arr = selectIds.split(',',-1);
		var act=zTree.transformToArray(nodes);
		$.each(act, function(i, item) {
			$.each(arr, function(i, id) {
				if(item.id==id){
					zTree.checkNode(item, true, true);
				}
			});
		});
	}
	$("#"+rootMenu).css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
	$("body").bind("mousedown", onBodyDown_body);
}
function hideMenu_body(rootMenu) {
	$("#"+rootMenu).fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown_body);
}
function onBodyDown_body(event) {
	if (!( event.target.id == "channelSelectName" || event.target.id == 'menuChannel' || $(event.target).parents("#menuChannel").length>0)) {
		treeMenu.hideMenu("menuChannel");
	}
	if (!( event.target.id == "channelSelectName2" || event.target.id == 'menuChannel2' || $(event.target).parents("#menuChannel2").length>0)) {
		treeMenu.hideMenu("menuChannel2");
	}
	if (!( event.target.id == "areaSelectName" || event.target.id == 'menuArea' || $(event.target).parents("#menuArea").length>0)) {
		treeMenu.hideMenu("menuArea");
	}
	if (!( event.target.id == "classSelectName" || event.target.id == 'menuClass' || $(event.target).parents("#menuClass").length>0)) {
		treeMenu.hideMenu("menuClass");
	}
}